package com.wang.mall.controller;

import com.wang.mall.bean.SysMenu;
import com.wang.mall.model.R;
import com.wang.mall.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {


    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public R update(@RequestBody SysMenu menu) {

        sysMenuService.updateById(menu);

        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:menu:save')")
    public R save(@RequestBody SysMenu menu) {

        sysMenuService.save(menu);

        return R.ok();
    }


    /**
     * 选择菜单(添加、修改菜单)
     */
    @GetMapping("/select")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public R select() {
        //查询列表数据
        List<SysMenu> menuList = sysMenuService.queryNotButtonList();

        //添加顶级菜单
        SysMenu root = new SysMenu();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);

        return R.ok(menuList);
    }


    @GetMapping("/info/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public R info(@PathVariable Long menuId) {
        return R.ok(sysMenuService.getById(menuId));
    }

    @PostMapping("/delete/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public R del(@PathVariable Long menuId) {
        boolean b = sysMenuService.removeById(menuId);
        return b ? R.ok() : R.fail("删除失败");
    }

    @GetMapping("/nav")
//    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Map<String, Object> findMenuByUserId() {

        List<SysMenu> menuList = sysMenuService.getUserMenuList();


        Set<String> permissions = sysMenuService.getPermissions(menuList.stream().map(SysMenu::getPerms).collect(Collectors.toSet()));

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("menuList", menuList);
        stringObjectHashMap.put("permissions", permissions);
        stringObjectHashMap.put("code", 0);
        return stringObjectHashMap;
    }


    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public List<SysMenu> list() {
        List<SysMenu> menuList = sysMenuService.list();
        for (SysMenu sysMenuEntity : menuList) {
            SysMenu parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
            if (parentMenuEntity != null) {
                sysMenuEntity.setParentName(parentMenuEntity.getName());
            }
        }

        return menuList;
    }


    /**
     * 获取当前用户的菜单
     * @return
     */
    @GetMapping("/listByCurrentUser")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public List<SysMenu> listByCurrentUser() {
        List<SysMenu> menuList = sysMenuService.listByCurrentUser();
        for (SysMenu sysMenuEntity : menuList) {
            SysMenu parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
            if (parentMenuEntity != null) {
                sysMenuEntity.setParentName(parentMenuEntity.getName());
            }
        }

        return menuList;
    }
}
